#ifndef WEBSOCKET_H
#define WEBSOCKET_H

#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <string>
#include <functional>
#include <thread>
#include <mutex>

class WebsocketClinet
{
  public:
    WebsocketClinet(const std::string & Host,const std::string& Req);
    virtual ~WebsocketClinet();
    void BootReadCallback(std::function<void(const char* data,std::size_t size)> func);
    //返回的指针由new分配
    std::pair<char*,std::size_t> ReadData();
    //不会转移指针所有权，也不会做修改
    void WriteData(const char*,std::size_t);
    void WriteText(const char*,std::size_t);
  private:
    std::function<void(const char* data,std::size_t size)> funcDataCallback;
    boost::asio::io_context ioc;
    boost::beast::websocket::stream<boost::asio::ip::tcp::socket> ws;
    std::jthread readMsgThread;
    void ReadMsg();
    std::mutex wsMut;
};

#endif
